package com.wan.service.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wan.entity.GameUser;
import com.wan.entity.LivingBuilding;
import com.wan.mapper.GameUserMapper;
import com.wan.mapper.LivingBuildingMapper;
import entity.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import utils.TimeUtils;

import java.util.Objects;

/**
 * <p>
 * 生活楼信息表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-03-11
 */
@Service
public class LivingBuildingService extends ServiceImpl<LivingBuildingMapper, LivingBuilding> {
    @Autowired
    private CollageService collageService;
    @Autowired
    private GameUserMapper gameUserMapper;
    @Value("${map.per}")
    private String per;

    @Transactional(rollbackFor = Exception.class)
    public ResultBean livingBuildSave(String accountNumber,String password,String nickName,String imageFile,Integer level,String signGe) {
        QueryWrapper<GameUser> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("account_number",accountNumber);
        GameUser user1 = gameUserMapper.selectOne(userQueryWrapper);
        if (Objects.nonNull(user1)){
            return new ResultBean(500,"账号已经存在");
        }
        GameUser user = new GameUser();
        user.setAccountNumber(accountNumber);
        user.setPassword(password);
        user.setNickName(nickName);

        String url = imageFile.substring(per.length());
        user.setFaceImg(url);
        user.setStatus(1);
        user.setLevel(level);
        user.setSignGe(signGe);
        user.setCreateTime(TimeUtils.GetNowDate("yyyy-MM-dd HH:mm:ss"));
        Integer i =gameUserMapper.insert(user);
        if (i == null){
            return new ResultBean(500,"游戏用户添加失败");
        }else {
            return new ResultBean(200,"游戏用户添加成功");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public ResultBean updateLivingBuild(Integer id,String accountNumber,String imageFile,String nickName,String password) {
        GameUser gameUser = gameUserMapper.selectById(id);
        gameUser.setAccountNumber(accountNumber);
        gameUser.setNickName(nickName);
        gameUser.setPassword(password);
        gameUser.setCreateTime(TimeUtils.GetNowDate("yyyy-MM-dd HH:mm:ss"));
        Integer i = gameUserMapper.updateById(gameUser);
        if (i == null){
            return new ResultBean(500,"更新失败");
        }else {
            return new ResultBean(200,"更新成功");
        }
    }
}
